package cn.xin.system.service.impl;

import cn.xin.base.query.BaseQuery;
import cn.xin.base.util.PageList;
import cn.xin.org.domain.Employee;
import cn.xin.system.domain.Menu;
import cn.xin.system.domain.Role;
import cn.xin.system.mapper.MenuMapper;
import cn.xin.system.mapper.PermissionMapper;
import cn.xin.system.mapper.RoleMapper;
import cn.xin.system.service.IRoleService;
import cn.xin.base.service.impl.BaseServiceImpl;
import cn.xin.system.vo.RoleAndMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xin
 * @since 2023-01-03
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 添加role和menu，role和permission的中间表数据
     * @param role
     */
    @Override
    public void add(Role role) {

        super.add(role);

        //添加role和menu的中间表数据
        menuMapper.addRoleMenu(role.getId(),role.getMenus());
        //添加role和permission的中间表数据
        permissionMapper.addRolePermission(role.getId(),role.getPermissions());
    }

//    @Override
//    public void update(Role role) {
//        super.update(role);
//        // 删除
//        menuMapper.deleteRoleMenu(role.getId());
//        // 菜单
//        menuMapper.addRoleMenu();
//
//        // 删除
//        permissionMapper.deleteRolePermission(role.getId());
//        // 按钮
//        permissionMapper.addRolePermission();
//    }
//
//    @Override
//    public void delete(Long id) {
//        super.delete(id);
//        menuMapper.deleteByEmpId(id);
//        permissionMapper.deleteByEmpId(id);
//    }

/*
    @Override
    public PageList<Role> queryByPage(BaseQuery query) {
        //获取Menu的集合
        PageList<Role> rolePageList = super.queryByPage(query);
        //判断menuPageList是否有数据
        if (rolePageList == null || CollectionUtils.isEmpty(rolePageList.getRows())){
            return rolePageList;
        }

        //获取menu的当前页的数据
        List<Role> roleList = rolePageList.getRows();
        //通过当前页获取role的id集合
        List<Long> roleIdList = roleList.stream().map(Role::getId).collect(Collectors.toList());

        //通过中间表的roleid获取到menuid的集合
        List<RoleAndMenuVo> roleAndMenuVos = menuMapper.findByMenuIdList(roleIdList);
        //对中间表就行分组 key为role的id，value为menu的id
        Map<Long,List<Long>> listMap = roleAndMenuVos.stream().collect(Collectors.groupingBy(RoleAndMenuVo::getRoleId,Collectors.mapping(RoleAndMenuVo::getMenuId,Collectors.toList())));




        roleList.forEach(role  -> {
            List<Long> list = listMap.get(role.getId());
            role.setMenus(list);

        });
        return rolePageList;

    }


 */
}

